<template>
  <div class="categories-page">
    <div class="page-header">
      <h1 class="stagger-item">分类管理</h1>
      <p class="page-description stagger-item">组织和浏览您的各类内容</p>
    </div>
    
    <div class="categories-actions">
      <div class="search-box stagger-item">
        <input 
          v-model="searchQuery" 
          type="text" 
          placeholder="搜索分类..." 
          class="search-input"
        >
        <span class="search-icon">🔍</span>
      </div>
      
      <button class="btn btn-primary stagger-item" @click="showAddCategory = true">
        <span class="btn-icon">+</span>
        新建分类
      </button>
    </div>
    
    <div class="categories-grid">
      <div 
        v-for="(category, index) in filteredCategories" 
        :key="category.id"
        class="category-card stagger-item"
        :style="{ 'animation-delay': `${index * 0.1}s` }"
        @click="selectCategory(category)"
      >
        <div class="category-icon">{{ category.icon }}</div>
        <div class="category-info">
          <h3 class="category-name">{{ category.name }}</h3>
          <p class="category-count">{{ category.itemCount }} 个项目</p>
        </div>
        <div class="category-actions">
          <button class="action-btn" @click.stop="editCategory(category)" title="编辑">✏️</button>
          <button class="action-btn" @click.stop="deleteCategory(category)" title="删除">🗑️</button>
        </div>
      </div>
    </div>
    
    <!-- 添加/编辑分类模态框 -->
    <div v-if="showAddCategory || editingCategory" class="modal-overlay">
      <div class="modal-content">
        <h3>{{ editingCategory ? '编辑分类' : '新建分类' }}</h3>
        
        <div class="form-group">
          <label>分类名称</label>
          <input v-model="newCategory.name" type="text" class="form-input">
        </div>
        
        <div class="form-group">
          <label>选择图标</label>
          <div class="icon-selector">
            <button 
              v-for="icon in availableIcons" 
              :key="icon"
              :class="['icon-option', { active: newCategory.icon === icon }]"
              @click="newCategory.icon = icon"
            >
              {{ icon }}
            </button>
          </div>
        </div>
        
        <div class="modal-actions">
          <button class="btn btn-secondary" @click="cancelEdit">取消</button>
          <button class="btn btn-primary" @click="saveCategory">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const searchQuery = ref('')
const showAddCategory = ref(false)
const editingCategory = ref(null)

const categories = ref([
  { id: 1, name: '工作文档', icon: '📄', itemCount: 24 },
  { id: 2, name: '个人笔记', icon: '📝', itemCount: 18 },
  { id: 3, name: '学习资料', icon: '📚', itemCount: 32 },
  { id: 4, name: '图片收藏', icon: '🖼️', itemCount: 45 },
  { id: 5, name: '音乐文件', icon: '🎵', itemCount: 12 },
  { id: 6, name: '视频资源', icon: '🎬', itemCount: 8 }
])

const newCategory = ref({
  name: '',
  icon: '📄'
})

const availableIcons = ['📄', '📝', '📚', '🖼️', '🎵', '🎬', '📊', '🔗', '📎', '📁']

const filteredCategories = computed(() => {
  if (!searchQuery.value) return categories.value
  return categories.value.filter(category => 
    category.name.toLowerCase().includes(searchQuery.value.toLowerCase())
  )
})

const selectCategory = (category) => {
  console.log('选中分类:', category.name)
}

const editCategory = (category) => {
  editingCategory.value = category
  newCategory.value = { ...category }
}

const deleteCategory = (category) => {
  if (confirm(`确定要删除分类"${category.name}"吗？`)) {
    categories.value = categories.value.filter(c => c.id !== category.id)
  }
}

const saveCategory = () => {
  if (!newCategory.value.name.trim()) {
    alert('请输入分类名称')
    return
  }
  
  if (editingCategory.value) {
    // 更新现有分类
    const index = categories.value.findIndex(c => c.id === editingCategory.value.id)
    if (index !== -1) {
      categories.value[index] = { ...newCategory.value, id: editingCategory.value.id }
    }
  } else {
    // 添加新分类
    const newId = Math.max(...categories.value.map(c => c.id), 0) + 1
    categories.value.push({
      ...newCategory.value,
      id: newId,
      itemCount: 0
    })
  }
  
  cancelEdit()
}

const cancelEdit = () => {
  showAddCategory.value = false
  editingCategory.value = null
  newCategory.value = { name: '', icon: '📄' }
}
</script>

<style scoped>
.categories-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  min-height: 100vh;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
}

.page-header h1 {
  font-size: 2.2rem;
  color: #1e293b;
  margin-bottom: 10px;
}

.page-description {
  font-size: 1.1rem;
  color: #64748b;
}

.categories-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  gap: 20px;
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 400px;
}

.search-input {
  width: 100%;
  padding: 12px 45px 12px 15px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 1rem;
}

.search-icon {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 1.2rem;
}

.btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  transition: all 0.3s ease;
}

.btn-primary {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  border: none;
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

.btn-primary:hover {
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.category-card {
  background: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 15px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.category-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-color: #cbd5e1;
}

.category-icon {
  font-size: 2rem;
  flex-shrink: 0;
}

.category-info {
  flex: 1;
}

.category-name {
  font-size: 1.2rem;
  color: #2c3e50;
  margin-bottom: 5px;
}

.category-count {
  color: #7f8c8d;
  font-size: 0.9rem;
}

.category-actions {
  display: flex;
  gap: 10px;
}

.action-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.1rem;
  padding: 5px;
  border-radius: 4px;
  transition: background 0.2s;
}

.action-btn:hover {
  background: #f8f9fa;
}

/* 模态框样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 30px;
  border-radius: 15px;
  width: 90%;
  max-width: 500px;
}

.modal-content h3 {
  margin-bottom: 25px;
  color: #2c3e50;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #495057;
}

.form-input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 1rem;
}

.icon-selector {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}

.icon-option {
  background: none;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  padding: 10px;
  cursor: pointer;
  font-size: 1.2rem;
  transition: all 0.2s;
}

.icon-option.active,
.icon-option:hover {
  border-color: #3498db;
  background: #e1f5fe;
}

.modal-actions {
  display: flex;
  gap: 15px;
  justify-content: flex-end;
  margin-top: 30px;
}

.btn-secondary {
  background: #6c757d;
  color: white;
}

.btn-secondary:hover {
  background: #5a6268;
}

@media (max-width: 768px) {
  .categories-page {
    padding: 15px;
  }
  
  .categories-actions {
    flex-direction: column;
    align-items: stretch;
  }
  
  .search-box {
    max-width: none;
  }
  
  .categories-grid {
    grid-template-columns: 1fr;
  }
  
  .modal-content {
    margin: 20px;
    width: calc(100% - 40px);
  }
  
  .icon-selector {
    grid-template-columns: repeat(4, 1fr);
  }
}
</style>